package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.entity.WechatAppInfoBean;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.widget.InputDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 微信：支付-》钱包
 */
public class WechatFundWalletActivity extends BaseActivity {

    @BindView(R2.id.iv_back)
    ImageView      ivBack;
    @BindView(R2.id.iv_linqian)
    ImageView      ivLinqian;
    @BindView(R2.id.tv_lingqian)
    TextView       tvLinqian;
    @BindView(R2.id.iv_arrow_linqian)
    ImageView      ivArrowLinqian;
    @BindView(R2.id.btn_linqian)
    RelativeLayout btnLinqian;
    @BindView(R2.id.iv_linqiantong)
    ImageView      ivLinqiantong;
    @BindView(R2.id.tv_tag_lingqiantong)
    TextView       tvTagLingqiantong;
    @BindView(R2.id.tv_lingqiantong_shouyilv)
    TextView       tvLingqiantongShouyilv;
    @BindView(R2.id.tv_lingqiantong)
    TextView       tvLingqiantong;
    @BindView(R2.id.iv_arrow_linqiantong)
    ImageView      ivArrowLinqiantong;
    @BindView(R2.id.btn_linqiantong)
    RelativeLayout btnLinqiantong;
    @BindView(R2.id.iv_yinhangka)
    ImageView      ivYinhangka;
    @BindView(R2.id.iv_qinshuka)
    ImageView      ivQinshuka;
    @BindView(R2.id.iv_anquanbaozhang)
    ImageView      ivAnquanbaozhang;
    @BindView(R2.id.tv_tip_bottom_1)
    TextView       tvTipBottom1;

    public WechatFundWalletActivity() {
        super(R.layout.activity_fund_wallet);
    }

    @Override
    protected void initView() {
        setStatusBarColor(Color.parseColor("#ededed"));
        setNavigationColor(Color.parseColor("#f7f7f7"));
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    @SuppressLint("SetTextI18n")
    private void refresh() {
        WechatAppInfoBean vo = WechatAppInfoBean.getWechat(WechatFundWalletActivity.this);
        tvLinqian.setText("¥" + vo.getLingqian());
        if (isLQTisZero(vo.getLingqiantong()))
            tvLingqiantong.setVisibility(View.GONE);
        else{
            tvLingqiantong.setVisibility(View.VISIBLE);
            tvLingqiantong.setText("¥" + vo.getLingqiantong());
        }
        tvLingqiantongShouyilv.setText("收益率" + vo.getLingqiantongRate() + "%");
    }

    @OnClick({R2.id.btn_linqian, R2.id.btn_linqiantong, R2.id.tv_lingqian,
            R2.id.tv_lingqiantong_shouyilv})
    public void onViewClicked(View view) {
        int i = view.getId();//点击了大ITEM
        //点击了数字
        if (i == R.id.btn_linqian || i == R.id.tv_lingqian) {
            InputDialog.showInputDialog(this, "编辑零钱余额", WechatAppInfoBean.getWechat(this).getLingqian(), EditorInfo.TYPE_CLASS_PHONE, new InputDialog.OnInputDialogListener() {
                @Override
                public void onCancelClicked() {
                }

                @Override
                public void onOKClicked(Dialog dialog, String inputText) {
                    try {
                        WechatAppInfoBean vo =
                                WechatAppInfoBean.getWechat(WechatFundWalletActivity.this);
                        String lingqian = MoneyFormat.formatMoney(inputText);
                        vo.setLingqian(lingqian);
                        WechatAppInfoBean.putOrUptate(WechatFundWalletActivity.this, vo);
                        dialog.dismiss();
                        refresh();
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastUtils.showLong("输入格式错误！");
                    }
                }
            });
        } else if (i == R.id.btn_linqiantong) {
            InputDialog.showInputDialog(this, "编辑零钱通余额", WechatAppInfoBean.getWechat(this).getLingqiantong(), EditorInfo.TYPE_CLASS_PHONE, new InputDialog.OnInputDialogListener() {
                @Override
                public void onCancelClicked() {
                }

                @Override
                public void onOKClicked(Dialog dialog, String inputText) {
                    try {
                        WechatAppInfoBean vo = WechatAppInfoBean.getWechat(WechatFundWalletActivity.this);
                        String lingqian = MoneyFormat.formatMoney(inputText);
                        vo.setLingqiantong(lingqian);
                        WechatAppInfoBean.putOrUptate(WechatFundWalletActivity.this, vo);
                        dialog.dismiss();
                        refresh();
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastUtils.showLong("输入格式错误！");
                    }
                }
            });
        } else if (i == R.id.tv_lingqiantong_shouyilv) {
            InputDialog.showInputDialog(this, "编辑零钱通的收益率", WechatAppInfoBean.getWechat(this).getLingqiantongRate(), EditorInfo.TYPE_CLASS_PHONE, new InputDialog.OnInputDialogListener() {
                @Override
                public void onCancelClicked() {

                }

                @Override
                public void onOKClicked(Dialog dialog, String inputText) {
                    try {
                        WechatAppInfoBean vo = WechatAppInfoBean.getWechat(WechatFundWalletActivity.this);
                        String lingqian = MoneyFormat.formatMoney(inputText);
                        vo.setLingqiantongRate(lingqian);
                        WechatAppInfoBean.putOrUptate(WechatFundWalletActivity.this, vo);
                        dialog.dismiss();
                        refresh();
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastUtils.showLong("输入格式错误！");
                    }
                }
            });
        }
    }

    private boolean isLQTisZero(String lingqiantong) {
        try {
            double v = Double.parseDouble(lingqiantong);
            if (v == 0)
                return true;
        } catch (Exception e) {
            ToastUtils.showShort("金额输入有误");
            e.printStackTrace();
        }
        return false;
    }

}
